package model.daos;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import model.db.DataBase;
import model.dtos.Player;
import model.dtos.Team;

public class PlayerDao implements GenericDao<Player>
{

	@Override
	public void save(Player dt)
	{
		TeamDao teamDao = new TeamDao();
		teamDao.findById(dt.getIdTeam()).addPlayer(dt);
	}

	@Override
	public Player findById(String id)
	{
		Player player = null;
		
		for (Team team : DataBase.getSeason().getTeams())
			for (Player ply : team.getPlayers())
				if (ply.getIdentification().equals(id))
					player = ply;
		
		return player;
	}

	@Override
	public List<Player> findAll()
	{
		List<Player> players = new ArrayList<Player>();
		
		for (Team t : DataBase.getSeason().getTeams())
			players.addAll(t.getPlayers());
		
		return players;
	}

	@Override
	public void deleteById(String id)
	{
		for (Team team : DataBase.getSeason().getTeams())
		{
			Iterator<Player> it = team.getPlayers().iterator();
			
			while (it.hasNext())
			{
				if (it.next().getIdentification().equals(id))
					it.remove();
			}
		}
	}

	@Override
	public void deleteAll()
	{
		for (Team t : DataBase.getSeason().getTeams())
			t.getPlayers().clear();
	}
	
	public void loadPlayersFromFile (String idTeam, File file) throws IOException, ParseException
	{
		TeamDao teamDao = new TeamDao();
		Team team = teamDao.findById(idTeam);
		BufferedReader bf = new BufferedReader(new FileReader(file));
		String line = null;
		while ((line = bf.readLine()) != null)
		{
			StringTokenizer tok = new StringTokenizer(line, ";");
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			
			Player player = new Player();
			player.setIdentification(tok.nextToken());
			player.setName(tok.nextToken());
			player.setLastName(tok.nextToken());
			player.setBirthday(df.parse(tok.nextToken()));
			player.setPosition(tok.nextToken());
			player.setIdTeam(idTeam);
			
			team.getPlayers().add(player);
		}
	}
	
	public Player playerMoreHrs()
	{
		
		List<Player> players = new ArrayList<Player>(this.findAll());
		
		Collections.sort(players, new Comparator<Player>() {
			
			public int compare(Player p1, Player p2)
			{
				return p1.getHrs() > p2.getHrs() ? -1 : (p1.getHrs() == p2.getHrs() ? 0 : 1);
			}
			
		});
		
		return players.get(0);
	}
	
	public Player playerLessHrs()
	{
		List<Player> players = new ArrayList<Player>(this.findAll());
		
		Collections.sort(players, new Comparator<Player>() {
			
			public int compare(Player p1, Player p2)
			{
				return p1.getHrs() < p2.getHrs() ? -1 : (p1.getHrs() == p2.getHrs() ? 0 : 1);
			}
		});
		
		return players.get(0);
	}
	
	public Player playerMoreRbis()
	{
		List<Player> players = new ArrayList<Player>(this.findAll());
		
		Collections.sort(players, new Comparator<Player>() {
			
			public int compare(Player p1, Player p2)
			{
				return p1.getRbis() > p2.getRbis() ? -1 : (p1.getRbis() == p2.getRbis() ? 0 : 1);
			}
		});
		
		return players.get(0);
	}
	
	public Player playerLessRbis()
	{
		List<Player> players = new ArrayList<Player>(this.findAll());
		
		Collections.sort(players, new Comparator<Player>() {
			
			public int compare(Player p1, Player p2)
			{
				return p1.getRbis() < p2.getRbis() ? -1 : (p1.getRbis() == p2.getRbis() ? 0 : 1);
			}
		});
		
		return players.get(0);
	}
}
